#ifndef __RECYCLE_QUEUE_H__
#define __RECYCLE_QUEUE_H__

#include "define.h"

//类模板不支持分离编译;
#include<iostream>


//定义一个模板;
template< class T >class RecycleQueue
{
public:
	RecycleQueue();
	~RecycleQueue();
	int init(size_t len);
	int PushRecycleQueue(T &task);
	T * PopRecycleQueue();
	int resetRecycleQueue();
	bool RecycleQueueIsEmpty();
	bool RecycleQueueIsFull();
private:
	T *queue;
	int head;
	int tail;
	size_t queueSize;
};

//构造函数
template< typename T > RecycleQueue< T >::RecycleQueue(){
	queue=NULL;
	head = tail = -1;
	queueSize = 0;
}


//析构函数
template< typename T> RecycleQueue< T >::~RecycleQueue(){
	if ( NULL != queue)
	{
		free(queue);
		queue = NULL;
	}
	head = tail = -1;
	queueSize = 0;
}

//初始化
template< typename T>
int RecycleQueue< T >::init(size_t len){
	try{
		queue = ( T *)malloc(sizeof(T)*len);
		memset(queue, '\0', sizeof(T)*len);
		head = tail = -1;
		queueSize = len;
		return RET::SUCCESS;
	}catch(std::bad_alloc){
		return RET::FAIL;
	}
}

//插入任务;
template< typename T >
int RecycleQueue< T >::PushRecycleQueue(T &task){
	if(RecycleQueueIsFull()){
#ifdef __DEBUG_ON__
		std::cout<< "The queue is Full!"<<std::endl;
#endif 
		return RET::FAIL;
	}else{
		queue[++head]=task;
		return RET::SUCCESS;
	}
}


template< typename T> 
T * RecycleQueue< T >::PopRecycleQueue(){
	T *temp = NULL;
	if(RecycleQueueIsEmpty()){
#ifdef __DEBUG_ON__
		std::cout<<"The queue is Empty!"<<std::endl;
#endif
	}else{
		temp = &(queue[++tail]);	
	}
	return temp;
}

//判断是否为空;
template< typename T >
bool RecycleQueue< T >::RecycleQueueIsEmpty(){
	if(head == tail&&head != -1){
		return true;
	}else{
		return false;
	}
}
//判断是否为满?
template< typename T >
bool RecycleQueue< T >::RecycleQueueIsFull(){
	if((head+1)%queueSize == tail){
		return true;
	}else{
		return false;
	}
}

#endif 
